package com.haojin.exer;

/**
 * @author .29.
 * @create 2022-03-30 13:48
 *
 * 练习：创建两个分线程，其中那个一个线程遍历100以内的偶数，另一个线程遍历100以内的奇数
 */

class Thread1 extends Thread{
    @Override
    public void run() {
        for(int i = 0;i < 100;i++){
            if(i%2 == 0){
                System.out.println(Thread.currentThread().getName()+" "+ i);
            }
        }
    }
}
class Thread2 extends Thread{
    @Override
    public void run() {
        for(int i = 0;i < 100;i++){
            if(i%2 != 0){
                System.out.println(Thread.currentThread().getName() +" "+ i);
            }
        }
    }
}
public class ThreadDemo {
    public static void main(String[] args) {
//        Thread1 thread1 = new Thread1();
//        Thread2 thread2 = new Thread2();
//        thread1.start();
//        thread2.start();

        //创建Thread类的匿名子类的方式
        new Thread(){
            @Override
            public void run() {
                for(int i = 0;i < 100;i++){
                    if(i%2 == 0){
                        System.out.println(Thread.currentThread().getName()+" "+ i);
                    }
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                for(int i = 0;i < 100;i++){
                    if(i%2 != 0){
                        System.out.println(Thread.currentThread().getName()+" "+ i);
                    }
                }
            }
        }.start();
    }


}
